import request from '@/utils/request'

// 扁平化数据转树形结构
export function formatData(data, parentId, cateLevel){
  let arr = [];
  data.map(async (vo)=>{
    if(vo.parentId == parentId){
      cateLevel = Number(cateLevel);
      cateLevel++;
      vo.children = await formatData(data, vo.cateCode, cateLevel);
      arr.push(vo);
      console.log(arr);
    }
  })
  return arr;
}

// 替换空格、空行等
export function formatContent(val){
  return val.replace(/&nbsp;/g,'')//替换&nbsp;
            .replace(/\r\n/g, '</span><br/><span>')//替换\r\n
            .replace(/\n/g,'</span><br><span>')//替换\n
            .replace(/\s/g, ' ')//替换空格、&nbsp;
            .replace(/\/n/g,'\n')//替换\n
}

// 替换换行符
export function resetContentVal(val){
  return val.replace(/<\/span><br><span>/g, '\n')
            .replace(/<\/span><br\/><span>/g, '\r\n')
}

// 文件大小转换
export function formatFileSize(size){
  let fileSize;
  if(size < 1024){
    fileSize = size + "B";
  }else if(size > 1024 && size < 1024*1024){
    fileSize = (size / 1024).toFixed(2) + "KB";	
  }else{
    fileSize = (size / 1024 / 1024).toFixed(2) + "MB";
  }
  return fileSize;
}

// 坐标系转换【WGS84转GCJ02】
export function wgs84ToGcj02(wgLat, wgLon) {
  var a = 6378245.0;
  var ee = 0.00669342162296594323;
  var pi = 3.14159265358979324;
  var dLat = transformLat(wgLon - 105.0, wgLat - 35.0);
  var dLon = transformLon(wgLon - 105.0, wgLat - 35.0);
  var radLat = wgLat / 180.0 * pi;
  var magic = Math.sin(radLat);
  magic = 1 - ee * magic * magic;
  var sqrtMagic = Math.sqrt(magic);
  dLat = (dLat * 180.0) / ((a * (1 - ee)) / (magic * sqrtMagic) * pi);
  dLon = (dLon * 180.0) / (a / sqrtMagic * Math.cos(radLat) * pi);
  var mgLat = wgLat + dLat;
  var mgLon = wgLon + dLon;
  return [mgLat, mgLon];
}

// 坐标系转换【GCJ02转WGS84】
export function gcj02ToWgs84(lat, lng) {
  var a = 6378245.0;
  var ee = 0.00669342162296594323;
  var pi = 3.14159265358979324;
  var dlat = transformlat(lng - 105.0, lat - 35.0)
  var dlng = transformlng(lng - 105.0, lat - 35.0)
  var radlat = lat / 180.0 * pi
  var magic = Math.sin(radlat)
  magic = 1 - ee * magic * magic
  var sqrtmagic = Math.sqrt(magic)
  dlat = (dlat * 180.0) / ((a * (1 - ee)) / (magic * sqrtmagic) * pi)
  dlng = (dlng * 180.0) / (a / sqrtmagic * Math.cos(radlat) * pi)
  var mglat = lat + dlat
  var mglng = lng + dlng
  return [lng * 2 - mglng, lat * 2 - mglat]
}

function transformLat(x, y) {
  var pi = 3.14159265358979324;
  var dLat = -100.0 + 2.0 * x + 3.0 * y + 0.2 * y * y + 0.1 * x * y + 0.2 * Math.sqrt(Math.abs(x));
  dLat += (20.0 * Math.sin(6.0 * x * pi) + 20.0 * Math.sin(2.0 * x * pi)) * 2.0 / 3.0;
  dLat += (20.0 * Math.sin(y * pi) + 40.0 * Math.sin(y / 3.0 * pi)) * 2.0 / 3.0;
  dLat += (160.0 * Math.sin(y / 12.0 * pi) + 320 * Math.sin(y * pi / 30.0)) * 2.0 / 3.0;
  return dLat;
}

function transformLon(x, y) {
  var pi = 3.14159265358979324;
  var dLon = 300.0 + x + 2.0 * y + 0.1 * x * x + 0.1 * x * y + 0.1 * Math.sqrt(Math.abs(x));
  dLon += (20.0 * Math.sin(6.0 * x * pi) + 20.0 * Math.sin(2.0 * x * pi)) * 2.0 / 3.0;
  dLon += (20.0 * Math.sin(x * pi) + 40.0 * Math.sin(x / 3.0 * pi)) * 2.0 / 3.0;
  dLon += (150.0 * Math.sin(x / 12.0 * pi) + 300.0 * Math.sin(x / 30.0 * pi)) * 2.0 / 3.0;
  return dLon;
}

// 获取当前项目
import store from '@/store';
import router from '@/router';
export function getCurrentProject(){
  let roles = store.getters.roles, projectId = null;
  if(roles.includes('R1') || roles.includes('ROLE_DEFAULT') || roles.includes('admin')){
    projectId = window.localStorage.getItem("uav_ui_current_project")?window.localStorage.getItem("uav_ui_current_project"):"";
  }else{
    projectId = router.history.current.query.projectId;
  }
  return projectId;
}